# -*- coding: utf-8 -*-
from datetime import datetime

from docxtpl import DocxTemplate
import pypandoc


class InfoForm:
    def __init__(self, name, sex, id_number, major, grade, education_level, party_entry_date, department,
                 political_affiliation):
        self.name = name
        self.sex = sex
        self.id_number = id_number
        self.major = major
        self.grade = grade
        self.education_level = education_level
        self.party_entry_date = party_entry_date
        self.department = department
        self.political_affiliation = political_affiliation


def main(infoForm):
    # 插入信息
    date = datetime.now().date().strftime("%Y年%m月%d日")

    tpl = DocxTemplate('模板/党员证明模板.docx')
    context = {
        'name': infoForm.name,
        'sex': infoForm.sex,
        'id_number': infoForm.id_number,
        'major': infoForm.major,
        'grade': infoForm.grade,
        'education_level': infoForm.education_level,
        'party_entry_date': infoForm.party_entry_date,
        'department': infoForm.department,
        'political_affiliation': infoForm.political_affiliation,
        'date': date
    }
    tpl.render(context)
    word_file = f'{infoForm.name}-{infoForm.id_number}.docx'
    # PDF_file = f'{infoForm.name}-{infoForm.id_number}.pdf'
    tpl.save(word_file)
    # pypandoc.convert_file(word_file, 'pdf', outputfile=PDF_file)  # 输出为PDF文件


if __name__ == '__main__':
    info = InfoForm(
        name="李道昱",
        sex='男',
        id_number='330702200109262316',
        major='计算机科学与技术',
        grade='2020',
        education_level='本科生',
        party_entry_date='2022年6月18日',
        department='本科生第一党支部',
        political_affiliation='正式党员'
    )
    main(info)
    print('文件已生成!')
